package com.example.balls;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;

public class MainActivity extends Activity {
	private final static Integer LENGHT = 6;
	private final static Integer BALL_POINT = 2;
	private Animation animScale;
	private Integer level = 0;
	private Integer[][] board = new Integer[][] {{0,0,0,0,0,0},
												 {0,0,0,0,0,0},
												 {0,0,0,0,0,0},
												 {0,0,0,0,0,0},
												 {0,0,0,0,0,0},
												 {0,0,0,0,0,0}};
	
	private final ImageButton[][] imgs = new ImageButton[LENGHT][LENGHT];
	private final Integer[] cells = new Integer[LENGHT];
	private ImageButton btnVolume;
	private Boolean volume;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        setClickListeners();
        
        btnVolume = (ImageButton) findViewById(R.id.imageVolume);
       	if(getIntent().getExtras()!=null && !getIntent().getExtras().isEmpty()) 
       		volume = getIntent().getExtras().getBoolean("volume");
        if(volume==null) volume = true;
       	
        if(volume) 
            btnVolume.setBackgroundResource(R.drawable.ic_volumeon);
        else
            btnVolume.setBackgroundResource(R.drawable.ic_volumeoff);
        
        btnVolume.setOnClickListener(new ImageButton.OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				volume = !volume;
		        refresh();
			}
            
        });
        
        final ImageButton btnExit = (ImageButton) findViewById(R.id.imgMenuExit);

        btnExit.setOnClickListener(new ImageButton.OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
		        finish();
			}
            
        }); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private void setClickListeners(){    	
    	for (int i = 0; i < LENGHT; i++) {
    		for (int j = 0; j < LENGHT; j++) {
    			setClick(imgs[i][j], cells[i], cells[j]);
    		}
		}

    	animScale.setAnimationListener(new Animation.AnimationListener(){        
    	    public void onAnimationEnd(Animation arg0) {
    	    }    	    
			public void onAnimationRepeat(Animation animation) { }
			public void onAnimationStart(Animation animation) {
				Intent intent = new Intent(MainActivity.this, BoardActivity.class);
				intent.putExtra("level", level);
				intent.putExtra("volume", volume);
				startActivity(intent);
			}
    	});
    }
    
    private void setClick(final ImageButton button, final Integer i, final Integer j){
    	button.setOnClickListener(new ImageButton.OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				level = i + j;
				board[i][j] = BALL_POINT;
				refresh();
				finish();
			}
        });
    }
    
    private void refresh(){
    	for (int i = 0; i < LENGHT; i++) {
    		for (int j = 0; j < LENGHT; j++) {
    			imgs[i][j].setBackgroundResource(R.drawable.ic_ball_bg);
    			if(board[i][j].equals(BALL_POINT)){
    				imgs[i][j].setBackgroundResource(R.drawable.ic_ball_green);
    				imgs[i][j].setAnimation(animScale);
    			}
    		}
		}
    	
    	if(volume){
    		btnVolume.setBackgroundResource(R.drawable.ic_volumeon);
    	}else{
    		btnVolume.setBackgroundResource(R.drawable.ic_volumeoff);
    	}
    }
    
    private void initialize(){
    	imgs[0][0] = (ImageButton) findViewById(R.id.btn00);
    	imgs[0][1] = (ImageButton) findViewById(R.id.btn01);
    	imgs[0][2] = (ImageButton) findViewById(R.id.btn02);
    	imgs[0][3] = (ImageButton) findViewById(R.id.btn03);
    	imgs[0][4] = (ImageButton) findViewById(R.id.btn04);
    	imgs[0][5] = (ImageButton) findViewById(R.id.btn05);

    	imgs[1][0] = (ImageButton) findViewById(R.id.btn10);
    	imgs[1][1] = (ImageButton) findViewById(R.id.btn11);
    	imgs[1][2] = (ImageButton) findViewById(R.id.btn12);
    	imgs[1][3] = (ImageButton) findViewById(R.id.btn13);
    	imgs[1][4] = (ImageButton) findViewById(R.id.btn14);
    	imgs[1][5] = (ImageButton) findViewById(R.id.btn15);

    	imgs[2][0] = (ImageButton) findViewById(R.id.btn20);
    	imgs[2][1] = (ImageButton) findViewById(R.id.btn21);
    	imgs[2][2] = (ImageButton) findViewById(R.id.btn22);
    	imgs[2][3] = (ImageButton) findViewById(R.id.btn23);
    	imgs[2][4] = (ImageButton) findViewById(R.id.btn24);
    	imgs[2][5] = (ImageButton) findViewById(R.id.btn25);

    	imgs[3][0] = (ImageButton) findViewById(R.id.btn30);
    	imgs[3][1] = (ImageButton) findViewById(R.id.btn31);
    	imgs[3][2] = (ImageButton) findViewById(R.id.btn32);
    	imgs[3][3] = (ImageButton) findViewById(R.id.btn33);
    	imgs[3][4] = (ImageButton) findViewById(R.id.btn34);
    	imgs[3][5] = (ImageButton) findViewById(R.id.btn35);

    	imgs[4][0] = (ImageButton) findViewById(R.id.btn40);
    	imgs[4][1] = (ImageButton) findViewById(R.id.btn41);
    	imgs[4][2] = (ImageButton) findViewById(R.id.btn42);
    	imgs[4][3] = (ImageButton) findViewById(R.id.btn43);
    	imgs[4][4] = (ImageButton) findViewById(R.id.btn44);
    	imgs[4][5] = (ImageButton) findViewById(R.id.btn45);

    	imgs[5][0] = (ImageButton) findViewById(R.id.btn50);
    	imgs[5][1] = (ImageButton) findViewById(R.id.btn51);
    	imgs[5][2] = (ImageButton) findViewById(R.id.btn52);
    	imgs[5][3] = (ImageButton) findViewById(R.id.btn53);
    	imgs[5][4] = (ImageButton) findViewById(R.id.btn54);
    	imgs[5][5] = (ImageButton) findViewById(R.id.btn55);
    	
    	animScale = AnimationUtils.loadAnimation(this, R.anim.scale);
    	
    	for (int i = 0; i < LENGHT; i++) {
			cells[i] = i;
		}
    }
}
